from django.db import models
from utils.models import BaseModel
# Create your models here.
class SaltAuth(BaseModel):
    id = models.AutoField(primary_key=True)
    saltname = models.CharField(max_length=50,verbose_name='salt名称',unique=True)
    salt_status = models.IntegerField(verbose_name='salt状态，0停用 1启用',help_text='salt状态，0停用 1启用')
    salt_isvalidate = models.IntegerField(verbose_name='salt 验证状态，0等待验证 1已验证 2 拒绝验证 3 已解除验证 ',help_text='salt 验证状态，0等待验证 1已验证 2 拒绝验证 3 已解除验证 ')
    salt_bind_status = models.IntegerField(verbose_name='salt与server主机关联 0 待绑定 1 已绑定',help_text='salt与server主机关联 0 待绑定 1 已绑定')
    salt_master = models.CharField(max_length=20,verbose_name='关联saltmaster',help_text='关联saltmaster')
    def __str__(self):
        return self.saltname
    class Meta:
        db_table = 'cmdb_salt_auth'


        verbose_name ="salt认证信息"
        verbose_name_plural = verbose_name


class Host(BaseModel):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20,verbose_name='服务器名称',unique=True,help_text='服务器名称')
    wlan = models.GenericIPAddressField(verbose_name='服务的外网IP',unique=True,help_text='服务的外网IP')
    lan = models.GenericIPAddressField(verbose_name='服务的内网IP',unique=True,help_text='服务的内网IP')
    salt = models.OneToOneField(SaltAuth,on_delete=models.CASCADE,null=True)
    status = models.IntegerField(verbose_name='服务的状态 0 停用 1启用',help_text='服务的状态 0 停用 1启用')
    system = models.IntegerField(verbose_name='系统 0 linux 1 windows 2 其他',help_text='系统 0 linux 1 windows 2 其他')

    def __str__(self):
        return self.name
    class Meta:
        db_table = 'cmdb_host'
        verbose_name ="主机信息"
        verbose_name_plural = verbose_name

class HostGroup(BaseModel):
    name = models.CharField(max_length=50,verbose_name='服务器组名',help_text='服务器组名')
    system = models.IntegerField(verbose_name='组系统 0 Linux 1 windows',help_text='组系统 0 Linux 1 windows' )
    status = models.IntegerField(verbose_name='组状态 0 停用 1 启用',help_text='组状态 0 停用 1 启用')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'cmdb_host_group'
        verbose_name = "主机组信息"
        verbose_name_plural = verbose_name
        permissions = (
            ("view_ServerGroup", "查看主机组信息"),
            ("add_ServerGroup", "添加主机组信息"),
            ("update_ServerGroup", "修改主机组信息"),
            ("delete_ServerGroup", "删除主机组信息"),
        )
class HostGroupMap(BaseModel):
    g_id = models.ForeignKey(HostGroup,on_delete=models.CASCADE)
    s_id = models.ForeignKey(Host,on_delete=models.CASCADE)

    class Meta:
        db_table = 'cmdb_host_group_map'
        verbose_name = "主机映射关系信息"
        verbose_name_plural = verbose_name




class SaltMaster(BaseModel):
    saltm_name = models.CharField(max_length=255,verbose_name='saltmaster名称',help_text='saltmaster名称')
    saltm_url = models.CharField(max_length=255, verbose_name='saltmaster url',help_text='saltmaster url')
    saltm_user = models.CharField(max_length=255, verbose_name='saltmaster 用户名',help_text='saltmaster 用户名')
    saltm_passwd = models.CharField(max_length=255, verbose_name='saltmaster 密码',help_text='saltmaster 密码')
    saltm_token = models.CharField(max_length=255, blank=True,null=True,verbose_name='saltmaster token',help_text='saltmaster token')
    saltm_expire = models.CharField(max_length=255, blank=True,null=True,verbose_name='saltmaster token过期时间',help_text='saltmaster token过期时间')
    saltm_update = models.CharField(max_length=255, blank=True,null=True,verbose_name='saltmaster token更新时间',help_text='saltmaster token更新时间')
    saltm_status = models.IntegerField(verbose_name='saltmaster 状态 1 启用 0 停用')
    def __str__(self):
        return self.saltm_name
    class Meta:
        db_table = 'cmdb_saltmaster'
        verbose_name = 'Saltmaster配置信息'
        verbose_name_plural = verbose_name

